#include <stdio.h>		// printf()
#include "sdl/sdl.h"
#include "srect.h"

// must be the header for SDL applications
int main(int argc, char ** argv)
{
// INIT
	printf("hello world!\n");
	// initializes SDL
	SDL_Init(SDL_INIT_EVERYTHING);
	// variables to describe the screen
	const int WIDTH = 640, HEIGHT = 480, BPP = 32;
	// creates the screen that we use, gives us a handle to the screen
	SDL_Surface * screen =
		SDL_SetVideoMode(WIDTH, HEIGHT, BPP, SDL_SWSURFACE);
	// sets the title of the screen
	SDL_WM_SetCaption("OMG!!!", 0);
	SRect r(10, 20, 30, 40);
	
	// velocity
	SPoint v;
	// the screen rectangle (to keep the moving rectangle bound to)
	SRect screenSize(0,0,WIDTH,HEIGHT);

	//r.x = 10;	r.y = 20;	r.w = 30;	r.h = 40;

	SDL_Event e;
	bool running = true;
// GAME LOOP
	while(running)
	{
		// INPUT - process all events in the event queue
		while(SDL_PollEvent(&e))
		{
			switch(e.type)
			{
			case SDL_QUIT:		running = false;	break;
			case SDL_KEYDOWN:	
				printf("%d ", e.key.keysym.sym);
				switch(e.key.keysym.sym)
				{
				case SDLK_UP:	v.y-=5;	break;
				case SDLK_RIGHT:v.x+=5;	break;
				case SDLK_LEFT:	v.x-=5;	break;
				case SDLK_DOWN:	v.y+=5;	break;
				}
				break;
			}
		}
		// LOGIC
		if(!SDL_PollEvent(0))SDL_Delay(20);
		// grab the rectangle's position (to add velocity to)
		SPoint loc = r.getPosition();
		// add the velocity to the position
		r.setPosition(loc.sum(v));
		// keep the rectangle bound to the screen
		r.keepBound(screenSize);

		// DRAW
		SDL_FillRect(screen, 0, SDL_MapRGB(screen->format,0,0,0));	// make the entire screen black
		SRect drawCopy(r);
		SDL_FillRect(screen, &drawCopy, 
			SDL_MapRGB(screen->format, 255, 0, 255));
		// required to swap the double-buffer
		SDL_Flip(screen);
	}
// RELEASE
	// cleans up the allocated memory from SDL_SetVideoMode
	SDL_FreeSurface(screen);
	// ensures that memory allocated using SDL_Init is freed
	SDL_Quit();
	return 0;
}